from autoclick import AutoClick


class App(object):
    
    def __init__(self):
        # 自动助手实例
        self.autoclick = AutoClick()
        # 用户信息页面
        self.home_page = 'https://pc.xuexi.cn/points/my-study.html'
        # 用户登录页面
        self.login_page = 'https://pc.xuexi.cn/points/my-study.html'
        # 每日答题页面
        self.exam_daily_page = 'https://pc.xuexi.cn/points/exam-practice.html'
        # 专项答题选择页面
        self.exam_special_page = 'https://pc.xuexi.cn/points/exam-paper-list.html'
        # 是否登录
        self.is_login = False
        # 任务获取积分
        self.mission_score = 0
        # 首次登录
        self.__login()

    # 用户登录
    def __login(self):
        self.autoclick.open(self.login_page)
        self.autoclick.execute('window.scrollTo(arguments[0],arguments[1])',0,1024)
        print('请使用移动客户端扫码登录')
        while not self.is_login:
            # 等待用户登录
            self.autoclick.sleep(10)
            if self.autoclick.current_url == self.home_page:
                self.is_login=True
                self.mission_score += 1
            else:
                # 超时未登录刷新页面
                self.autoclick.refresh()
        print('【登录任务】已完成,总计获得：{}分'.format(self.mission_score))


    # 答题任务列表
    def exams(self):
        # 点击我要答题
        self.autoclick.select('xpath','/html/body/div[1]/div/div[2]/div/div/div/div[1]/div/a[2]/div/div[2]/div[1]/span').click()
        # 每日答题
        self.exam_daily()
        # 专项答题
        self.exam_special()

    # 每日答题
    def exam_daily(self):
        # 点击每日答题
        self.autoclick.execute("document.getElementsByClassName('blocks')[0].children[0].click()")
        print('正在进行任务：【每日答题】')
        is_opend = False
        while not is_opend:
            if self.exam_daily_page == self.autoclick.current_url:
                is_opend = True
            else:
                self.autoclick.sleep(5)
                self.autoclick.refresh()
        # 解答题目
        for n in range(5):
            self.autoclick.sleep(3)
            # 获取题型
            exam_type = self.autoclick.execute('return document.getElementsByClassName("q-header")[0].innerText')
            # 打开答案提示框
            self.autoclick.execute("document.getElementsByClassName('tips')[0].click()")
            # 提示个数，部分提多个答案
            exam_tips_length = self.autoclick.execute("return document.getElementsByClassName('line-feed')[0].children.length")
            # 答案列表
            exam_tips = []
            anwser = '正确'
            for i in range(exam_tips_length):
                exam_tips.append(self.autoclick.execute("return document.getElementsByClassName('line-feed')[0].children[arguments[0]].innerText",i))
            # 关闭答案提示框
            self.autoclick.execute("document.getElementsByClassName('tips')[0].click()")
            # 输入答案
            if exam_type == '填空题':
                tips = ''
                if len(exam_tips)==0:
                    tips='观看视频'
                    anwser = '错误'
                else:
                    for t in exam_tips:
                        tips += t
                self.autoclick.select('css selector','#app > div > div.layout-body > div > div.detail-body > div.question > div.q-body > div > input').send_keys(tips)
            if exam_type == '单选题':
                chose = ''
                choosable_length = self.autoclick.execute("return document.getElementsByClassName('q-answers')[0].children.length")
                # 判断答案
                for j in range(choosable_length):
                    chose = self.autoclick.execute("return document.getElementsByClassName('q-answers')[0].children[arguments[0]].innerText",j).split('.')[1]
                    right = True
                    for i in range(len(exam_tips)):
                        if chose.find(exam_tips[i]) == -1:
                            right = False
                            break
                    # 选择正确答案✔
                    if right:
                        self.autoclick.execute("document.getElementsByClassName('q-answers')[0].children[arguments[0]].click()",j)
                        break
            if exam_type == '多选题':
                choosable_length = self.autoclick.execute("return document.getElementsByClassName('q-answers')[0].children.length")
                for k in range(choosable_length):
                    chose = self.autoclick.execute("return document.getElementsByClassName('q-answers')[0].children[arguments[0]].innerText",k).split('.')[1]
                    right = True
                    for i in range(len(exam_tips)):
                        if chose.find(exam_tips[i]) != -1:
                            # 选择正确答案✔
                            self.autoclick.execute("document.getElementsByClassName('q-answers')[0].children[arguments[0]].click()",k)
            # 点击确定
            self.autoclick.execute("document.getElementsByTagName('button')[0].click()")
            print('{} / 5 ：题型：{exam_type}\t答案：{exam_tips}\t答题结果：{anwser}】'.format((n+1),exam_type=exam_type,exam_tips=exam_tips,anwser=anwser))
        self.autoclick.sleep(2)
        self.autoclick.screen_shot('每日答题')
        self.mission_score += 5
        print('【每日答题任务】已完成,总计获得：{}分'.format(self.mission_score))
        self.autoclick.execute("document.getElementsByTagName('a')[2].click()")
        self.autoclick.sleep(2)
        self.autoclick.switch_window()


    # 专项答题
    def exam_special(self):
        # 点击专项答题
        self.autoclick.execute("document.getElementsByClassName('blocks')[0].children[2].click()")
        print('正在进行任务：【专项答题】')
        is_opend = False
        while not is_opend:
            if self.exam_special_page == self.autoclick.current_url:
                is_opend = True
            else:
                self.autoclick.sleep(5)
                self.autoclick.refresh()
        for p in range(9):
            # 一页可供答题的item数量
            self.autoclick.sleep(2)
            one_page_exam_length = self.autoclick.execute("return document.getElementsByClassName('items')[0].children.length")
            for i in range(one_page_exam_length):
                choosable = self.autoclick.execute("return document.getElementsByClassName('items')[0].children[arguments[0]].children[1].innerText",i)
                print(choosable)
                if choosable == '开始答题' or choosable == '继续答题':
                    self.autoclick.execute("document.getElementsByClassName('items')[0].children[arguments[0]].children[1].children[0].click()",i)
                    # 解答题目
                    for n in range(10):
                        self.autoclick.sleep(2)
                        # 获取题型
                        exam_type = self.autoclick.execute('return document.getElementsByClassName("q-header")[0].innerText')[:3]
                        # 打开答案提示框
                        self.autoclick.execute("document.getElementsByClassName('tips')[0].click()")
                        # 提示个数，部分提多个答案
                        exam_tips_length = self.autoclick.execute("return document.getElementsByClassName('line-feed')[0].children.length")
                        # 答案列表
                        exam_tips = []
                        anwser = '正确'
                        for i in range(exam_tips_length):
                            exam_tips.append(self.autoclick.execute("return document.getElementsByClassName('line-feed')[0].children[arguments[0]].innerText",i))
                        # 关闭答案提示框
                        self.autoclick.execute("document.getElementsByClassName('tips')[0].click()")
                        # 输入答案
                        if exam_type == '填空题':
                            tips = ''
                            if len(exam_tips)==0:
                                tips='观看视频'
                                anwser = '错误'
                            else:
                                for t in exam_tips:
                                    tips += t
                            self.autoclick.select('css selector','#app > div > div.layout-body > div > div.detail-body > div.question > div.q-body > div > input').send_keys(tips)
                        if exam_type == '单选题':
                            chose = ''
                            choosable_length = self.autoclick.execute("return document.getElementsByClassName('q-answers')[0].children.length")
                            # 判断答案
                            for j in range(choosable_length):
                                chose = self.autoclick.execute("return document.getElementsByClassName('q-answers')[0].children[arguments[0]].innerText",j).split('.')[1]
                                right = True
                                for i in range(len(exam_tips)):
                                    if chose.find(exam_tips[i]) == -1:
                                        right = False
                                        break
                                # 选择正确答案✔
                                if right:
                                    self.autoclick.execute("document.getElementsByClassName('q-answers')[0].children[arguments[0]].click()",j)
                                    break
                        if exam_type == '多选题':
                            choosable_length = self.autoclick.execute("return document.getElementsByClassName('q-answers')[0].children.length")
                            for k in range(choosable_length):
                                chose = self.autoclick.execute("return document.getElementsByClassName('q-answers')[0].children[arguments[0]].innerText",k).split('.')[1]
                                right = True
                                for i in range(len(exam_tips)):
                                    if chose.find(exam_tips[i]) != -1:
                                        # 选择正确答案✔
                                        self.autoclick.execute("document.getElementsByClassName('q-answers')[0].children[arguments[0]].click()",k)
                        # 点击确定
                        self.autoclick.execute("document.getElementsByTagName('button')[0].click()")
                        print('{} / 10 ：题型：{exam_type}\t答案：{exam_tips}\t答题结果：【{anwser}】'.format((n+1),exam_type=exam_type,exam_tips=exam_tips,anwser=anwser))
                    # 提交
                    self.autoclick.execute("document.getElementsByClassName('submit-btn')[0].click()")
                    self.autoclick.sleep(3)
                    self.autoclick.screen_shot('专项答题')
                    self.mission_score += 10
                    print('【专项答题任务】已完成,总计获得：{}分'.format(self.mission_score))
                    return
            self.autoclick.execute("document.getElementsByClassName('ant-pagination-item-link')[1].click()")
            self.autoclick.sleep(2)    
        print('暂无专项答题可完成')

app = App()
app.exams()